<?php
namespace app\com\controller;
use  think\Controller;
use  app\com\model\Teacher;
use think\Request;

class SmallTeacher extends Controller
{
    public function index()
    {

$smallteacher = new Teacher;

$smallteachers = $smallteacher ->select();
        /*$smallteacherss = $smallteachers[0];
        var_dump($smallteacherss->getData('name'));
        return $smallteacherss->getData('name');*/

        $this ->assign('smallteachers',$smallteachers);

      $html =  $this ->fetch();
        return $html;

    }

    //插入数据
    public function insert(){
        //新建测试数据
        /*$teacher = [];
        $teacher['name']='王五';
        $teacher['username']='wangwu';
        $teacher['sex']='男';
        $teacher['email']='1144174065@qq.com';
        var_dump($teacher);*/
        //应用teacher数据表对应的模型
/*        $Teacher = new Teacher();*/
        //向teacher表中插入数据并判断是否插入成功
/*       $Teacher ->data($teacher) ->save();*/
/*        return $teacher['name'].'成功添加';*/
//面向对象重写
        //Request::instance()返回一个对象，调用这个对象的post方法就可以获得提交的数据；
        $post = Request::instance() ->post();
        $Teacher = new Teacher();
        $Teacher ->name =$post['name'];
        $Teacher ->sex =$post['username'];
        $Teacher ->username =$post['sex'];
        $Teacher ->email =$post['email'];
        $result = $Teacher ->validate(true) ->save($Teacher ->getData());
        //反馈结果
        if($result===false){

            return '新增失败'.$Teacher ->getError();
        }else{

            return '新增成功,新增ID为：'.$Teacher ->id;
        }
        return $Teacher ->name.'成功添加至数据库中'.'其ID为'.$Teacher ->id;

    }
    //用户添加数据
    public  function add(){

        $html = $this ->fetch();
        return $html;
        //接收传入的数据
        //实例化Teacher空对象
        //为对象赋值
        //新增对象至数据表
        //反馈结果
    }
    //新建删除action方法
    public function delet(){

        //获取要删除的对象
/*$Teacher = Teacher::get('10');
        if(!is_null($Teacher)){

            if($Teacher ->delete()){
                return '删除成功';
            }
        }else{

            return '删除失败';
        }*/

        //删除对象

        //第二种
        /*var_dump(Request::instance()->param());
        return ;
        if($state = Teacher::destroy('10')){

            return '成功删除'.$state.'条数据';
        }else{

            return '删除失败';
        }*/
// 获取pathinfo传入的ID值.
        /*$id = Request::instance()->param('id/d'); // “/d”表示将数值转化为“整形”

        if (is_null($id) || 0 === $id) {
            return $this->error('未获取到ID信息');
        }

        // 获取要删除的对象
        $Teacher = Teacher::get($id);

        // 要删除的对象不存在
        if (is_null($Teacher)) {
            return $this->error('不存在id为' . $id . '的教师，删除失败');
        }

        // 删除对象
        if (!$Teacher->delete()) {
            return $this->error('删除失败:' . $Teacher->getError());
        }

        // 进行跳转
        return $this->success('删除成功', url('index'));*/
     $id =  Request::instance() ->param('id/d');
        if(is_null($id) || 0===$id){

            return $this ->error('未获取到ID信息');
        }
        //获取要删除的对象
      $Teacher =  Teacher::get($id);

        if(is_null($Teacher)){
            return $this ->error('不存在id为'.$id.'的教师，删除失败');
        }
        if(!$Teacher ->delete()){

            return $this->error('删除失败'.$Teacher->getError());
        }
        //进行跳转
        return $this ->success('删除成功',url('index'));
    }
    //新建edit的action
    public  function edit(){
        //获取传入ID
        $id =  Request::instance()->param('id/d');
        //在Teacher模型表中获取当前记录
       $Teacher = Teacher::get($id);
        //将数据传给V层
        $this ->assign('Teacher',$Teacher);
     $html =   $this ->fetch();
        return $html;
        //获取封装好的V层内容
        //将封装好的V层内容返回给用户


        return ;
        //新建updata的action

    }
    public function update(){

     $post =  Request::instance()->post();
        var_dump($post);

    }
}
